package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setMeal")
@Slf4j
public class SetMealController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetMealService setMealService;

    @RequestMapping("/getToken")
    public Result getToken(){
        String ak = "YIPDaZe-gj9nxq2HzunNE2WQbgmd53sFlyM0xRbS";
        String sk = "Kh153VpGzonM-_oKMY8I-xqcCO6-Be5hxKx934yv";
        Auth auth = Auth.create(ak, sk);
        String token = auth.uploadToken("xiaomage-health");
        log.debug("套餐图片上传获取token成功！");
        return new Result(true, "获取token成功",token);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        log.debug("SetMealController: add:　setmeal　===> "+setmeal);
        log.debug("SetMealController: add:　checkGroupIds　===> "+checkGroupIds);
        setMealService.add(setmeal,checkGroupIds);
        log.debug("新增检查套餐成功");
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("SetMealController: findByPage:　queryPageBean　===> "+queryPageBean);
        PageResult pageResult = setMealService.findByPage(queryPageBean);
        log.debug("SetMealController: findByPage:　pageResult　===> "+pageResult);
        log.debug("分页查询检查套餐成功");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("SetMealController: delById:　id　===> "+id);
        setMealService.delById(id);
        log.debug("删除检查套餐成功");
        return new Result(true,"删除检查套餐成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("SetMealController: findById:　id　===> "+id);
        Setmeal setmeal = setMealService.findById(id);
        log.debug("SetMealController: findById:　setmeal　===> "+setmeal);
        log.debug("根据id查询检查套餐成功");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @RequestMapping("/findCheckGroupIdsById")
    public Result findCheckGroupIdsById(Integer id){
        log.debug("SetMealController: findCheckGroupIdsById:　id　===> "+id);
        List<Integer> checkGroupIdsList = setMealService.findCheckGroupIdsById(id);
        log.debug("SetMealController: findCheckGroupIdsById:　checkGroupIdsList　===> "+checkGroupIdsList);
        log.debug("根据id查询检查套餐关联检查项成功");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,checkGroupIdsList);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        log.debug("SetMealController: edit:　setmeal　===> "+setmeal);
        log.debug("SetMealController: edit:　checkGroupIds　===> "+checkGroupIds);
        setMealService.edit(setmeal,checkGroupIds);
        log.debug("编辑检查套餐成功");
        return new Result(true,"编辑检查套餐成功");
    }

    @RequestMapping("/addImgName.do")
    public Result addImgName(String newName){
        log.debug("SetMealController: addImgName: newName ===> "+ newName);
        jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_RESOURCES,newName);
        log.debug("图片名称添加到redis成功");
        return new Result(true,"图片名称添加到redis成功");
    }

}
